const basiAuth = require("basic-auth")
const jwt = require("jsonwebtoken")
const result = require("../config/hand")
const security = require('./tokentime').security
class showtoken {
    constructor() {}
    get m() {
        return async (ctx, next) => {
            // console.log("show",ctx.req)
            const token = ctx.req.headers.authorization;
            if (!token) {
                throw new result({
                    errorcode: '401',
                    msg: "没有访问权限"
                }, 401)
            }
            try {
                var authcode = jwt.verify(token, security.secretKey)
                // console.log("authcode",authcode)
            } catch (error) {
                // console.log("进入error后", error)
                if (error.name == 'TokenExpiredError' || error == "jwt expired") {
                    console.log("进入了判断")
                    throw new result({
                        error: '401',
                        msg: '账号已过期,请重新登录'
                    }, 401)
                }
                throw new result({
                    error: '401',
                    msg: '没有访问权限'
                }, 401)
            }
            ctx.basiAuth = {
                uid: authcode.uid
            }
            await next()
        }
    }
}

module.exports = {
    showtoken
}